package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_ItemDao;
import com.rc.net.NetSupport;

public class ItemSync {

	private Context context;
	private SQLiteDatabase db;
	private DaoSession daoSession;
	public String query;
	private tbl_ItemDao daoItem;
	public Cursor cursor;
	public static String[] keys = { "status", "ItemID", "messages" };

	public ItemSync(Context context, SQLiteDatabase db, DaoSession daoSession) {
		this.context = context;
		this.daoSession = daoSession;
		this.db = db;
		daoItem = this.daoSession.getTbl_ItemDao();
	}

	/**
	 * Delete multi Item
	 */
	public void deleteItem() {
		new callDeleteItem().execute("");
	}

	/**
	 * Insert Item
	 */
	public void insertItem() {
		new callInsertItem().execute("");
	}

	/**
	 * Update Item
	 */
	public void updateItem() {
		new callUpdateItem().execute("");
	}

	/**
	 * call multidelteItem
	 */
	private class callDeleteItem extends
			AsyncTask<String, Void, ArrayList<Long>> {

		public callDeleteItem() {
			super();
		}

		@Override
		protected ArrayList<Long> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ItemDao.Properties.IsDelete.columnName + " = '1'";
			ArrayList<tbl_Item> arrItem;
			ArrayList<Long> arrItemID = new ArrayList<Long>();

			// get data in sqlite
			cursor = db.query(daoItem.getTablename(), daoItem.getAllColumns(),
					query, null, null, null, null);
			arrItem = new ArrayList<tbl_Item>();

			// get ArrayList Item by cursor
			arrItem = tbl_Item.getObjectbyCursor(cursor);
			if (arrItem.size() > 0) {
				for (tbl_Item objItem : arrItem) {
					// get ID Item
					arrItemID.add(objItem.getItemID());
				}
				// query server
				if (NetSupport.deleteItems(arrItemID, context)) {
					return arrItemID;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<Long> arrItemID) {
			insertItem();
		}
	}

	/**
	 * call multiInsertItem
	 */
	private class callInsertItem extends
			AsyncTask<String, Void, ArrayList<tbl_Item>> {

		@Override
		protected ArrayList<tbl_Item> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ItemDao.Properties.IsCreate.columnName + " = '1'";
			ArrayList<tbl_Item> arrItem;

			// get data in sqlite
			cursor = db.query(daoItem.getTablename(), daoItem.getAllColumns(),
					query, null, null, null, null);
			arrItem = new ArrayList<tbl_Item>();

			// get ArrayList Item by cursor
			arrItem = tbl_Item.getObjectbyCursor(cursor);
			if (arrItem.size() > 0) {

				// query server
				if (NetSupport.insertItems(arrItem, context)) {
					return arrItem;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Item> arrItemID) {
			updateItem();
		}
	}

	/**
	 * call Update item
	 */
	private class callUpdateItem extends
			AsyncTask<String, Void, ArrayList<tbl_Item>> {

		@Override
		protected ArrayList<tbl_Item> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ItemDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_Item> arrItem;

			// get data in sqlite
			cursor = db.query(daoItem.getTablename(), daoItem.getAllColumns(),
					query, null, null, null, null);
			arrItem = new ArrayList<tbl_Item>();

			// get ArrayList Item by cursor
			arrItem = tbl_Item.getObjectbyCursor(cursor);
			if (arrItem.size() > 0) {

				// query server
				if (NetSupport.updateItems(arrItem, context)) {
					return arrItem;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Item> arrItemID) {

		}
	}
}
